/********************************************************************************
* Copyright (c) 2009, Matteo Pelati (matteo.pelati@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Matteo Pelati ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/

#include "stdafx.h"
#include <iostream>
#include <exception>


#include "DbEnvironment.h"
#include "DbXmlManager.h"
#include "DbXmlContainer.h"
#include "DbException.h"
#include "DbXmlException.h"
#include "DbXmlModify.h"
#include "DbXmlQueryExpression.h"
#include "DbXmlResults.h"
#include "DbXmlTransaction.h"



namespace BerkeleyDB { namespace Xml
{

	void DbXmlModify::AddAppendStep(DbXmlQueryExpression^ sel, DbXmlModifyObjectType type, String^ name, String^ content, int location)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			CLRSTR2STDSTR(ccontent, content);
			n_mod->addAppendStep(*(sel->GetInnerObject()), (XmlModify::XmlObject) type, cname, ccontent, location);
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddAppendStep(DbXmlQueryExpression^ sel, DbXmlModifyObjectType type, String^ name, DbXmlValueCollection^ content, int location)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_mod->addAppendStep(*(sel->GetInnerObject()), (XmlModify::XmlObject) type, cname, *(content->GetInnerObject()), location);
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddInsertAfterStep(DbXmlQueryExpression^ sel, DbXmlModifyObjectType type, String^ name, String^ content)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			CLRSTR2STDSTR(ccontent, content);
			n_mod->addInsertAfterStep(*(sel->GetInnerObject()), (XmlModify::XmlObject) type, cname, ccontent);
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddInsertAfterStep(DbXmlQueryExpression^ sel, DbXmlModifyObjectType type, String^ name, DbXmlValueCollection^ content)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_mod->addInsertAfterStep(*(sel->GetInnerObject()), (XmlModify::XmlObject) type, cname, *(content->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddInsertBeforeStep(DbXmlQueryExpression^ sel, DbXmlModifyObjectType type, String^ name, String^ content)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			CLRSTR2STDSTR(ccontent, content);
			n_mod->addInsertBeforeStep(*(sel->GetInnerObject()), (XmlModify::XmlObject) type, cname, ccontent);
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddInsertBeforeStep(DbXmlQueryExpression^ sel, DbXmlModifyObjectType type, String^ name, DbXmlValueCollection^ content)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_mod->addInsertBeforeStep(*(sel->GetInnerObject()), (XmlModify::XmlObject) type, cname, *(content->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddRemoveStep(DbXmlQueryExpression^ sel)
	{
		try
		{
			n_mod->addRemoveStep(*(sel->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddRenameStep(DbXmlQueryExpression^ sel, String^ newname)
	{
		try
		{
			CLRSTR2STDSTR(cnewname, newname);
			n_mod->addRenameStep(*(sel->GetInnerObject()), cnewname);
		}
		rethrowDbXmlException();
	}
	
	void DbXmlModify::AddUpdateStep(DbXmlQueryExpression^ sel, String^ text)
	{
		try
		{
			CLRSTR2STDSTR(ctext, text);
			n_mod->addUpdateStep(*(sel->GetInnerObject()), ctext);
		}
		rethrowDbXmlException();
	}
	
	int DbXmlModify::Execute(DbXmlValue^ val, DbXmlQueryContext^ qctx, DbXmlUpdateContext^ uctx)
	{
		try
		{
			return n_mod->execute(*(val->GetInnerObject()), *(qctx->GetInnerObject()), *(uctx->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	int DbXmlModify::Execute(DbXmlValueCollection^ val, DbXmlQueryContext^ qctx, DbXmlUpdateContext^ uctx)
	{
		try
		{
			return n_mod->execute(*(val->GetInnerObject()), *(qctx->GetInnerObject()), *(uctx->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	int DbXmlModify::Execute(DbXmlTransaction^ ctx, DbXmlValue^ val, DbXmlQueryContext^ qctx, DbXmlUpdateContext^ uctx)
	{
		if (ctx == nullptr) return Execute(val, qctx, uctx);
		try
		{
			return n_mod->execute(*(ctx->GetInnerObject()), *(val->GetInnerObject()), *(qctx->GetInnerObject()), *(uctx->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	int DbXmlModify::Execute(DbXmlTransaction^ ctx, DbXmlValueCollection^ val, DbXmlQueryContext^ qctx, DbXmlUpdateContext^ uctx)
	{
		if (ctx == nullptr) return Execute(val, qctx, uctx);
		try
		{
			return n_mod->execute(*(ctx->GetInnerObject()), *(val->GetInnerObject()), *(qctx->GetInnerObject()), *(uctx->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	

	

}}